﻿Option Explicit On
Option Strict Off

Imports System.IO
Imports System.IO.IsolatedStorage

Module mdPersistence

  Sub ClearIS()
    Try

      Dim subDirName As String = String.Format("Calculin")

      Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()

      SyncLock store
        Try

          If Not store.DirectoryExists(subDirName) Then Exit Try

          store.DeleteDirectory(subDirName)

        Catch ex As Exception
        Finally

        End Try

      End SyncLock

    Catch ex As Exception

    End Try
  End Sub

  Sub Persist(ByVal Values As String, ByVal KeyName As String)
    Try
      Dim subDirName As String = String.Format("Calculin\Resultados\{0:0000}{1:00}{2:00}", Now.Year, Now.Month, Now.Day).ToString

      Dim skey As String = KeyName

      Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()

      Dim sFile As String = String.Format("{0}\{1}.txt", subDirName, skey)

      SyncLock store
        Try

          If Not store.DirectoryExists(subDirName) Then store.CreateDirectory(subDirName)

          Dim IOSTREAM As IsolatedStorageFileStream
          If Not store.FileExists(sFile) Then
            IOSTREAM = store.CreateFile(sFile)
            Dim sEnc As String = String.Format("Soluciones para {0} {1} {2} {1}", KeyName, vbCrLf, Now.ToString()).ToString
            IOSTREAM.Write(System.Text.Encoding.UTF8.GetBytes(sEnc), 0, System.Text.Encoding.UTF8.GetBytes(sEnc).Length)
          Else
            IOSTREAM = store.OpenFile(sFile, IO.FileMode.Append)
          End If


          IOSTREAM.Write(System.Text.Encoding.UTF8.GetBytes(Values), 0, System.Text.Encoding.UTF8.GetBytes(Values).Length)

          If IOSTREAM.Length = 0 Then
            IOSTREAM.Close()
            store.DeleteFile(sFile)
            Exit Try
          End If

          IOSTREAM.Close()
          IOSTREAM.Dispose()
          
        Catch ex As Exception
          store.DeleteFile(sFile)
        Finally

        End Try

      End SyncLock

    Catch ex As Exception

    End Try
  End Sub

  Function Hidrate(ByVal KeyName As String) As String

    Dim result As String = String.Empty

    Try
      Dim subDirName As String = String.Format("Calculin\Resultados\{0:0000}{1:00}{2:00}", Now.Year, Now.Month, Now.Day).ToString

      Dim skey As String = KeyName

      Dim store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()

      Dim sFile As String = String.Format("{0}\{1}.txt", subDirName, skey)

      SyncLock store
        Try

          If Not store.DirectoryExists(subDirName) Then store.CreateDirectory(subDirName)

          Dim IOSTREAM As IsolatedStorageFileStream
          If store.FileExists(sFile) Then
            IOSTREAM = store.OpenFile(sFile, IO.FileMode.Open)
            Dim DataAy(CInt(IOSTREAM.Length) - 1) As Byte
            IOSTREAM.Read(DataAy, 0, CInt(IOSTREAM.Length) - 1)
            result = System.Text.Encoding.UTF8.GetString(DataAy, 0, DataAy.Length)
            IOSTREAM.Close()
            IOSTREAM.Dispose()
          End If
         
        Catch ex As Exception
          store.DeleteFile(sFile)
        Finally

        End Try

      End SyncLock

    Catch ex As Exception

    End Try

    Return result

  End Function
End Module
